package scau.online.web.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.fasterxml.jackson.databind.deser.Deserializers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scau.cty.base.common.BaseController;
import scau.cty.base.common.IBaseService;
import scau.cty.dictionary.Code;
import scau.cty.entity.Question;
import scau.cty.service.IQuestionService;

import java.math.BigDecimal;
import java.util.*;
/**
 * Created by caoxiong on 2017/4/19.
 * 管理员 问题反馈管理器
 */
@Api(value = "问题反馈管理控制器",description = "问题反馈接口")
@RestController
@RequestMapping(value = "/admin_question_feedback", produces = "application/json")
public class AdminQuestionFeedbackController extends BaseController{
    @Autowired
    private IBaseService<Question> questionIBaseService;

    /**
     * 获取已反馈问题列表 根据pageSize和pageNow查询 order为排序方式
     * @param pageSize
     * @param pageNow
     * @param order
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "反馈问题列表接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageNow",value = "请求页码",required = true,paramType = "query"),
            @ApiImplicitParam(name = "order",value = "排序方式，不传参数或参数为0为默认排序，其他情况按照username排序",required = true,paramType = "query")
    })
    @GetMapping(value = "/question_list")
    public String getQuestionFeedbackList(Integer pageSize,Integer pageNow,Integer order) throws Exception{
        Map map = new LinkedHashMap();
        if(pageNow == null || pageNow == 0){
            pageNow = 1;
        }
        if(pageSize == null || pageSize ==0){
            sendCode(map, Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        if(order == null || order == 0){
            //默认排序
            List<Question> questionList = questionIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"id");
            map.put("questionList",questionList);
        }else {
            List<Question> questionList = questionIBaseService.selectPageEntityOrderByProperty(pageNow,pageSize,"username");
            map.put("questionList",questionList);
        }
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 通过反馈问题的 id 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除反馈问题接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "问题唯一标识id",required = true,paramType = "query")})
    @PostMapping(value = "/question_delete")
    public String deleteQuestionById(String id){
        Map map = new LinkedHashMap();
        if(id == null || id == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return  JSON.toJSONString(map);
        }
        int deleteQuestion = questionIBaseService.delete(id);
        map.put("deleteQuestion",deleteQuestion);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }

    /**
     * 增加问题 id 和 时间 自动生成
     * @param question
     * @return
     */
    @ApiOperation(value = "增加反馈问题接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "query"),
            @ApiImplicitParam(name = "questionContent",value = "反馈问题内容",required = true,paramType = "query")})
    @PostMapping(value = "/question_add")
    public String addQuestion(Question question){
        Map map = new LinkedHashMap();
        if(question.getUsername() == null || question.getUsername() == "" || question.getQuestionContent() == null || question.getQuestionContent() == ""){
            sendCode(map,Code.ILLEGAL_DATA_ERROR);
            return JSON.toJSONString(map);
        }
        question.setCreateTime(new Date());
        int addQuestion = questionIBaseService.add(question);
        map.put("addQuestion",addQuestion);
        sendCode(map,Code.OPERATION_SUCCESS);
        return JSON.toJSONString(map);
    }
}
